/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Bean;

import Action.ContactSessionBeanLocal;
import Entity.Contact;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import java.util.Date;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 *
 * @author Cyrax
 */
@ManagedBean
@SessionScoped
public class ContactBean {

    @EJB
    private ContactSessionBeanLocal contactSessionBean;
    private Contact con;
    private Contact selectedContact;
    private List<Contact> listcon;

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getFromMail() {
        return fromMail;
    }

    public void setFromMail(String fromMail) {
        this.fromMail = fromMail;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    private String fromMail;
    private String password;
    private String content;
    private String subject;

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    /** Creates a new instance of ContactBean */
    public ContactBean() {
        con = new Contact();
    }

    /**
     * @return the con
     */
    public Contact getCon() {
        return con;
    }

    public Contact getSelectedContact() {
        return selectedContact;
    }

    /**
     * @param con the con to set
     */
    public void setSelectedContact(Contact selectedContact) {
        this.selectedContact = selectedContact;
    }

    public void setCon(Contact con) {
        this.con = con;
    }

    public List<Contact> getViewContact() {
        listcon = contactSessionBean.viewAllContact();
        return listcon;
    }

    public void deleteContact(Contact au) {
        contactSessionBean.deleteContact(au);
        //return "AboutUs.xhtml";
    }

    public void updateContact() {

        int Id = con.getCtID();
        contactSessionBean.updateContact(Id, con.getFirstName(), con.getYourEmail(), con.getMessage(), con.getCtDate(), con.getStatus());
    }

    public void addContact() {
        contactSessionBean.addContact(con);
    }

    public void addContactShrink() {
        Date date = new Date();
        con.setCtDate(date);
        con.setStatus(Boolean.TRUE);
        contactSessionBean.addContact(con);
    }
    private String key;

    public List<Contact> getsearchContact() {

//        if (searchType.equals("admin")) {

        listcon = contactSessionBean.searchContact(getKey().toString());

//        }

        return listcon;

    }

    /**
     * @return the key
     */
    public String getKey() {
        return key;
    }

    /**
     * @param key the key to set
     */
    public void setKey(String key) {
        this.key = key;
    }


    public void sendReplyContact() throws Exception {
// java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
        Properties props = System.getProperties();
// –
        String to =selectedContact.getYourEmail();
        String subject = getSubject() + " - EasyTickets.com";
        String body = getContent();
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.port", "587");
        props.put("mail.smtp.starttls.enable", "true");
        final String login = getFromMail()+"@gmail.com";//”nth001@gmail.com”;//usermail
        final String pwd = getPassword();//”password cua ban o day”;
        Authenticator pa = null; //default: no authentication
        if (login != null && pwd != null) { //authentication required?
            props.put("mail.smtp.auth", "true");
            pa = new Authenticator() {

                public PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(login, pwd);
                }
            };
        }//else: no authentication
        Session session = Session.getInstance(props, pa);
// — Create a new message –
        Message msg = new MimeMessage(session);
// — Set the FROM and TO fields –
        msg.setFrom(new InternetAddress(getFromMail()));
        msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));

// — Set the subject and body text –
        msg.setSubject(subject);
        msg.setText(body);
// — Set some other header information –
        msg.setHeader("X-Mailer", "LOTONtechEmail");
        msg.setSentDate(new Date());
        msg.saveChanges();
// — Send the message –
        Transport.send(msg);
        System.out.println("Message sent OK.");

    }
}
